package com.lt.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.lt.report.service.UploadExecutor;
import com.lt.report.util.VeDate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

/**
 * @Description 上传文件
 * @Author yz
 */
@Controller
@Slf4j
public class UploadController {

    @Autowired
    UploadExecutor uploadExecutor;

    @GetMapping("/uploadIndex")
    public String upload() {
        return "index";
    }

    @GetMapping("/uploadAllFile")
    public String uploadAllFile() {
        return "uploadAllFile";
    }

//    @PostMapping(value = "/upload")
//    @ResponseBody
//    public String uploadExcel(HttpServletRequest request) {
//        log.info("\n【请求地址】：{}\n【请求时间】：{}\n", "/upload", VeDate.getNowDate());
//        try {
//            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//            MultipartFile file = multipartRequest.getFile("filename");
//            if (file.isEmpty()) {
//                return "文件不能为空";
//            }
//            InputStream inputStream = file.getInputStream();
//            log.info("\n【请求地址】：{}\n【请求时间】：{}\n【上传文件名】：{}", "/upload", VeDate.getNowDate(), file.getOriginalFilename());
//            String result = importServiceImpl.getBankListByExcel(inputStream, file.getOriginalFilename());
//            inputStream.close();
//            System.gc();
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "上传失败。";
//        }
//    }

    @PostMapping(value = "/upload")
    @ResponseBody
    public String uploadExcel(HttpServletRequest request) {
        log.info("\n【请求地址】：{}\n【请求时间】：{}\n", "/upload", VeDate.getNowDate());
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = multipartRequest.getFile("imageFile");
            if (file.isEmpty()) {
                return "文件不能为空";
            }
            InputStream inputStream = file.getInputStream();
            log.info("\n【请求地址】：{}\n【请求时间】：{}\n【上传文件名】：{}", "/upload", VeDate.getNowDate(), file.getOriginalFilename());
            uploadExecutor.executeUpload(inputStream, file.getOriginalFilename());
            System.gc();
            JSONObject object = new JSONObject();
            object.put("result","true");
            return object.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败。";
        }
    }


}

